package com.yangrui.app.util;

import com.yangrui.app.entity.LessonLog;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

@Slf4j
public class LogUtil {
    @SneakyThrows
    public static List<LessonLog> parseLog(String log) {
        List<LessonLog> lessonLogs = new ArrayList<>();
        BufferedReader reader = new BufferedReader(new StringReader(log));
        String line;
        while((line = reader.readLine()) != null) {
            if(line.matches("\\d{4}-\\d{2}-\\d{2}.*")){
                String[] arr = line.split("\\| ");
                LessonLog lessonLog = new LessonLog();
                lessonLog.setDate(arr[0].trim());
                lessonLog.setLevel(arr[1].trim());
                lessonLog.setThread(arr[2].trim());
                lessonLog.setClassName(arr[3].substring(0, arr[3].indexOf(".")));
                lessonLog.setMethodName(arr[3].substring(arr[3].indexOf(".") + 1, arr[3].indexOf(":")));
                lessonLog.setLineNumber(arr[3].substring(arr[3].indexOf(":") + 1));
                lessonLog.setMessage(arr[4].trim());
                lessonLogs.add(lessonLog);
            }

        }
        reader.close();
        return lessonLogs;
    }
}
